package cc.vicp.djx314.starfood.web.user;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cc.vicp.djx314.starfood.domain.account.User;
import cc.vicp.djx314.starfood.domain.order.Order;
import cc.vicp.djx314.starfood.service.order.OrderService;
import cc.vicp.djx314.starfood.web.exception.UserNotSameAsSessionException;
import cc.vicp.djx314.starfood.web.exception.UserSessionTimeOutException;

@Controller
public class UserInfoOrderController {
	@Autowired
	OrderService orderService;

	@RequestMapping(value = "/user/orders/{userId}", method = RequestMethod.GET)
	public String showUserOrderInfo(@PathVariable("userId") Integer userId,
			Model model, HttpSession session)
					throws UserSessionTimeOutException, UserNotSameAsSessionException {
		User user = (User) session.getAttribute("loginUser");
		if (user == null) {
			throw new UserSessionTimeOutException("用户session过时");
		}
		if (userId.equals(user.getId()) == false) {
			throw new UserNotSameAsSessionException("请求的用户id:  " + userId +
					"  与session的用户id:  " + user.getId() + "  不符");
		}
		List<Order> orders = orderService.getOrdersInfoForUser(user);
		model.addAttribute("orders", orders);
		return "customer/user/userinfoorder";
	}
}